package com.jiantong.work.impl.service.yangzhibaoxian;

/**
 * Created by ThinkPad on 2017/9/5.
 */

import com.google.common.base.Throwables;
import com.jiantong.work.impl.dao.yangzhibaoxian.InsuranceApplicationDao;
import com.jiantong.work.impl.dao.yangzhibaoxian.InsuranceAreaDao;
import com.jiantong.work.model.yangzhibaoxian.Inform;
import com.jiantong.work.model.yangzhibaoxian.InsuranceArea;
import com.jiantong.work.service.yangzhibaoxian.InsuranceAreaService;
import io.terminus.boot.rpc.common.annotation.RpcProvider;
import io.terminus.common.model.PageInfo;
import io.terminus.common.model.Paging;
import io.terminus.common.model.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 保险员工负责区域表
 */
@Slf4j
@Service
@RpcProvider
public class InsuranceAreaServiceImpl implements InsuranceAreaService {
    private InsuranceAreaDao insuranceAreaDao;

    @Autowired
    public InsuranceAreaServiceImpl(InsuranceAreaDao insuranceAreaDao) {
        this.insuranceAreaDao = insuranceAreaDao;
    }
    @Override
    public Response<InsuranceArea> findById(Long id) {
        try {
            InsuranceArea insuranceArea = (InsuranceArea) insuranceAreaDao.findById(id);
            if (insuranceArea == null) {
                log.error("insuranceArea(id={}) is not found", id);
                return Response.fail("insuranceArea.not.found");
            }

            return Response.ok(insuranceArea);
        } catch (Exception e) {
            log.error("failed to find insuranceArea(id={}), cause:{}", id, Throwables.getStackTraceAsString(e));
            return Response.fail("insuranceArea.find.fail");
        }
    }

    @Override
    public Response<List<InsuranceArea>> findByIds(List ids) {
        try {
            List<InsuranceArea> insuranceAreaList =  insuranceAreaDao.findByIds(ids);
            if (insuranceAreaList == null) {
                log.error("insuranceAreaList(ids={}) is not found", ids);
                return Response.fail("insuranceAreaList.not.found");
            }

            return Response.ok(insuranceAreaList);
        } catch (Exception e) {
            log.error("failed to find insuranceAreaList(ids={}), cause:{}", ids, Throwables.getStackTraceAsString(e));
            return Response.fail("insuranceAreaList.find.fail");
        }

    }

    @Override
    public Response<List<InsuranceArea>> list(Map map) {
        try {
            List<InsuranceArea> insuranceAreaList = insuranceAreaDao.list(map);
            if (insuranceAreaList == null) {
                log.error("insuranceAreaList(ids={}) is not found", map);
                return Response.fail("insuranceAreaList.not.found");
            }

            return Response.ok(insuranceAreaList);
        } catch (Exception e) {
            log.error("failed to find insuranceAreaList(ids={}), cause:{}", map, Throwables.getStackTraceAsString(e));
            return Response.fail("insuranceAreaList.find.fail");
        }
    }

    @Override
    public Response<Paging<InsuranceArea>> ping(Integer offset, Integer limit, Map map) {
        try {
            PageInfo pageInfo=new PageInfo(offset,limit);
            Paging<InsuranceArea> insuranceAreaPaging =  insuranceAreaDao.paging(pageInfo.getOffset(),pageInfo.getLimit(), map);
            if (insuranceAreaPaging == null) {
                log.error("insuranceAreaPaging(ids={}) is not found", map);
                return Response.fail("insuranceAreaPaging.not.found");
            }

            return Response.ok(insuranceAreaPaging);
        } catch (Exception e) {
            log.error("failed to find insuranceAreaPaging(ids={}), cause:{}", map, Throwables.getStackTraceAsString(e));
            return Response.fail("insuranceAreaPaging.find.fail");
        }
    }

    @Override
    public Boolean update(InsuranceArea insuranceArea) {
        try {
            Boolean b =  insuranceAreaDao.update(insuranceArea);
            return b;
        } catch (Exception e) {
            return false;
        }
    }

    @Override
    public Long create(InsuranceArea insuranceArea) {
        try {
            insuranceAreaDao.create( insuranceArea);
            return  insuranceArea.getId();
        } catch (Exception e) {
            e.printStackTrace();
            return -500L;
        }
    }

    @Override
    public Integer creates(List<InsuranceArea> insuranceAreaList) {
        try {

            return  insuranceAreaDao.creates(insuranceAreaList);
        } catch (Exception e) {
            e.printStackTrace();
            return -500;
        }
    }

    @Override
    public InsuranceArea findByAreaCode(String area) {
        return insuranceAreaDao.findByAreaCode(area);
    }
}
